// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: daniel

#pragma once

#include <sstream>
#include <string>

namespace FIX {
class Message;
class FieldMap;
}  // namespace FIX

namespace coin2::exchange::base::api_base::fix {

/********************************************************************************
 * class FixMessageUtils                                                        *
 ********************************************************************************/
class FixMessageUtils {
 public:
  static std::string ToJson(const std::string&);
  static std::string ToJson(const FIX::Message&);
  template <typename ReturnType, typename ValueType>
  static ReturnType ConvertTo(ValueType&&);

 private:
  static std::string ToJsonFields(const FIX::FieldMap&, int);
};

// TODO(daniel) - add type traits filter
template <typename ReturnType, typename ValueType>
ReturnType FixMessageUtils::ConvertTo(ValueType&& value) {
  try {
    ReturnType ret;
    std::stringstream ss;
    ss << value;
    ss >> ret;
    return ret;
  } catch (std::exception& ex) {
    return ReturnType{};
  }
}

}  // namespace coin2::exchange::base::api_base::fix
